#include "RenderData.h"
#include <iostream>

namespace k2graphics
{

RenderData::RenderData(int target, int maxB, int maxI, int maxF)
	: renderTarget(target),
	numBytes(0), numInts(0), numFloats(0),
	maxBytes(maxB), maxInts(maxI), maxFloats(maxF)
{
	byteData = new unsigned char[maxBytes];
	intData = new int[maxInts];
	floatData = new float[maxFloats];
	customData = NULL;
}

RenderData::~RenderData()
{
	delete[] byteData;
	delete[] intData;
	delete[] floatData;
}

void RenderData::pushByte(unsigned char b)
{
	if(numBytes >= maxBytes)
		return;

	byteData[numBytes] = b;
	++numBytes;
}

void RenderData::pushInt(int i)
{
	if(numInts >= maxInts)
		return;

	intData[numInts] = i;
	++numInts;
}

void RenderData::pushFloat(float f)
{
	if(numFloats >= maxFloats)
		return;

	floatData[numFloats] = f;
	++numFloats;
}

void RenderData::setByte(int index, unsigned char b)
{
	if(index < 0 || index >= maxBytes)
		return;

	byteData[index] = b;

	if(index >= numBytes)
		numBytes = index + 1;
}

void RenderData::setInt(int index, int i)
{
	if(index < 0 || index >= maxInts)
		return;

	intData[index] = i;

	if(index >= numInts)
		numInts = index + 1;
}

void RenderData::setFloat(int index, float f)
{
	if(index < 0 || index >= maxFloats)
		return;

	floatData[index] = f;

	if(index >= numFloats)
		numFloats = index + 1;
}

void RenderData::debugPrintData()
{
	std::cout << "Target: " << renderTarget << std::endl;
	std::cout << "Ints: " << numInts << ", Bytes: " << numBytes << ", Floats: " << numFloats << std::endl;
	for(int i = 0; i < numInts; ++i)
		std::cout << intData[i] << " ";
	std::cout << std::endl;
	for(int i = 0; i < numBytes; ++i)
		std::cout << (int)(byteData[i]) << " ";
	std::cout << std::endl;
	for(int i = 0; i < numFloats; ++i)
		std::cout << floatData[i] << " ";
	std::cout << std::endl;
}

void RenderData::genTestData(int s)
{
	renderTarget = s * 2;
	for(int i = 0; i < 5; ++i)
		byteData[i] = i * 50 + s;
	for(int i = 0; i < 3; ++i)
		intData[i] = -i * 12 + s;
	for(int i = 0; i < 4; ++i)
		floatData[i] = s * 3.2317263615 / ((float)i + 0.2);
	numBytes = 5;
	numInts = 3;
	numFloats = 4;
}

}
